<?php namespace App\Http\Controllers;

use App\Models\Role;
use App\User;
use App\Models\Permission;

class HomeController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Home Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders your application's "dashboard" for users that
	| are authenticated. Of course, you are free to change or remove the
	| controller as you wish. It is just here to get your app started!
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
// 		$this->middleware('auth');
	}

	/**
	 * Show the application dashboard to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
		return view('home');
	}

	public  function addRole() {
	    $owner = new Role();
	    $owner->name         = 'owner';
	    $owner->display_name = 'Project Owner'; // optional
	    $owner->description  = 'User is the owner of a given project'; // optional
	    $owner->save();

	    $admin = new Role();
	    $admin->name         = 'admin';
	    $admin->display_name = 'User Administrator'; // optional
	    $admin->description  = 'User is allowed to manage and edit other users'; // optional
	    $admin->save();
	}

	public function linkRole() {
	    $user = User::where('name', '=', 'wangjun')->first();


	    // role attach alias
	    $user->attachRole("2"); // parameter can be an Role object, array, or id

	    // or eloquent's original technique
// 	    $user->roles()->attach($user->id);
	}

	public function rolePermission() {
	    $createPost = new Permission();
	    $createPost->name         = 'create-post';
	    $createPost->display_name = 'Create Posts'; // optional
	    // Allow a user to...
	    $createPost->description  = 'create new blog posts'; // optional
	    $createPost->save();

	    $editUser = new Permission();
	    $editUser->name         = 'edit-user';
	    $editUser->display_name = 'Edit Users'; // optional
	    // Allow a user to...
	    $editUser->description  = 'edit existing users'; // optional
	    $editUser->save();

	    $admin = Role::where("name","=","admin")->first();

	    $admin->attachPermission($createPost);
	    // equivalent to $admin->perms()->sync(array($createPost->id));
	    $owner = Role::where("name","=","owner")->first();
	    $owner->attachPermissions(array($createPost, $editUser));
	}

}
